% \documentclass{memoir}
% \pagestyle{empty} % 移除页眉页脚
% \setlrmarginsandblock{0pt}{0pt}{*} % 左右边距设为 0
% \setulmarginsandblock{0pt}{0pt}{*} % 上下边距设为 0
% \checkandfixthelayout % 应用布局设置
\documentclass{article}
% \documentclass{standalone}
\usepackage{algorithmicx}
\usepackage{geometry}
\geometry{
    % a4paper,.
    top=1cm,
    % top=2.2cm, bottom=2.2cm, 
    bottom=1cm,
    left=0cm, right=0cm,
}

\pagestyle{empty} %取消页码数

\usepackage[linesnumbered,ruled,vlined,algo2e]{algorithm2e}
% \usepackage[linesnumbered,ruled,vlined,algo2e,breakable]{algorithm2e}
% \SetAlgoLined：增添end行
% \DontPrintSemicolon：不显示行末尾的分号
% \SetKwInput{KwInput}{Input}：设置输入
% \SetKwInOut{KwInput}{Input}：设置输入输出
% ruled：标题左对齐，顶部和底部各添加一条线
% linesnumbered：显示行编号
\usepackage{ctex} %注意，这里必须引入ctex,否则中文内容无法输出
\SetKwRepeat{Do}{do}{while}
\SetKwFunction{Try}{try}
\SetKwFunction{Catch}{catch}
% \SetKwProg{TryCatch}{Try}{Catch}{}

\begin{document}
% \begin{adjustbox}{max totalsize={\textwidth}{\textheight}}
\begin{algorithm2e}[htbp]
    \SetAlgoLined
    \renewcommand{\thealgocf}{4-1} %自定义算法编号
    \caption{AST抽象语法树-复杂度计算}
    \SetKwInOut{KwIn}{\textbf{输入}}
    \SetKwInOut{KwOut}{\textbf{输出}}	

    % \KwIn{源码文件内容:$src\_content$}
    % \KwOut{函数复杂度分数:$complexity$}

    \KwIn{source file content:$src\_content$}
    \KwOut{function complexity fraction:$complexity$}

    node $\gets$ ast.parse(src\_content)

    % 副函数开始
    \SetKwFunction{FMain}{generic\_visit}
    \SetKwProg{Fn}{Function}{}{}
        \Fn{\FMain{self,node}}{
            % Call(update\_max\_line(node)) \\
            self.update\_max\_line(node) //更新当前节点最大行数\\
            self.calculate\_node\_complexity(node) //计算当前节点复杂度\\
            super().generic\_visit(node) //调用父类\\
        }\textbf{end}

        \SetKwFunction{FMain}{update\_max\_line}
        \SetKwProg{Fn}{Function}{}{}
            \Fn{\FMain{self,node}}{
                \If{ \text{Call}(hasattr(node, 'lineno')) }{
                % \If{ has }{
                    self.max\_line $\gets$ node.lineno //更新当前该节点最大行数 \\
                }
            }\textbf{end}
    
        \SetKwFunction{FMain}{calculate\_node\_complexity}
        \SetKwProg{Fn}{Function}{}{}
            \Fn{\FMain{self,node}}{
            % node\_type=GetClass(node).GetName(node) \\
            node\_type $\gets$ Type(node) //获取节点类型 \\
            // 定义复杂度规则映射表 \\
            \texttt{complexity\_rules} $\gets$ $\{$ \\
            % \Indp
            \hspace*{1em} Try：Len(node.handlers)+Bool(node.orelse)\\
            \hspace*{1em} TryExcept：Len(node.handlers)+Bool(node.orelse) \\
            \hspace*{1em} BoolOp：Len(node.values)-1 \\
            \hspace*{1em} If：1 \\
            \hspace*{1em} IfExp：1 \\
            \hspace*{1em} Match：Call(calculate\_match\_complexity(node)) \\
            \hspace*{1em} For：Bool(node.orelse)+1 \\
            \hspace*{1em} While：Bool(node.orelse)+1 \\
            \hspace*{1em} AsyncFor：Bool(node.orelse)+1 \\
            \hspace*{1em} comprehension：Bool(node.orelse)+1 \\
                % \hspace*{1em} $Try\:len(handlers) + bool(orelse),$\\
            % \Indm
                % $\text{len(handlers)} + \text{bool(orelse)}$,\;
             \\ $\}$

            \If{node\_type in complexity\_rules}{
                % complexity
                Add\_Score(self.complexity,Get\_Score(node\_type Match complexity\_rules)) //复杂度分数累加
            }
            }\textbf{end}

            \SetKwFunction{FMain}{calculate\_match\_complexity}
            \SetKwProg{Fn}{Function}{}{}
                \Fn{\FMain{self,node}}{
                    % cases:List $\gets$ \textbf{for}  case \textbf{in}  node.cases \\
                    if\_else\_case $\gets$ Flase //初始化标识 \\
                    \For{case \textbf{in} node.cases}{
                        % only if\;
        
                        \If{Call(getattr(case.pattern,"pattern", None)) is None}{
                            if\_else\_case $\gets$ True
                        }
                    }
                    \Return{MAX(0,Len(node.cases)-INT(if\_else\_case))} //计算多个case条件 \\
            }\textbf{end}

% \end{adjustbox}
\end{algorithm2e}
\end{document}    